//
//  OtherWorksViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/20.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class OtherWorksViewController: UIViewController {

    fileprivate var otherWorks: [OtherWorksModel]?
    
    fileprivate lazy var collectionView: UICollectionView = {
       let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 10
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionV.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.register(OtherWorksTotalCell.self, forCellWithReuseIdentifier: "OtherWorksTotalCell")
        return collectionV
    }()
    
    convenience init(otherWorks: [OtherWorksModel]?) {
        self.init()
        self.otherWorks = otherWorks
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        self.view.addSubview(self.collectionView)
        navigationItem.title  = "其他作品"
        setUpSnap()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.barStyle = .black
        navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "nav_bg"), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        
    }

    fileprivate func setUpSnap() {
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.usnp.edges)
        }
    }

}

extension OtherWorksViewController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return otherWorks?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor((collectionView.frame.width - 40) / 3)
        return CGSize(width: width, height: 200)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OtherWorksTotalCell", for: indexPath) as! OtherWorksTotalCell
        cell.model = otherWorks?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.collectionView.deselectItem(at: indexPath, animated: true)
        guard let model = otherWorks?[indexPath.row] else { return }
        let vc = ComicsDetailViewController(comicid: model.comicId)
        navigationController?.pushViewController(vc, animated: true)
    }
}




